﻿using System;
using System.Collections.Generic;
using OASys.Biz.BizAccess;
using OASys.Entity;
using OASys.Entity.EntitySearch;
using OASys.Enum;
using OASys.Web.Base;
using Project.Common;
using Project.Web.Base.Utility;

namespace OASys.Web.Areas.ModuleExecutive.Models
{
	/// <summary>
	/// 请假信息维护管理
	/// </summary>
    public class ModelApplyHolidayIndex : BaseModel
    {
        private BizUTApplyHoliday bizUTApplyHoliday;

        private SearchApplyHoliday searchEntity;
        /// <summary>
        /// 搜索条件
        /// </summary>
        public SearchApplyHoliday SearchEntity
        {
            get
            {
                if (searchEntity == null)
                    searchEntity = new SearchApplyHoliday();
                if (string.IsNullOrEmpty(searchEntity._OrderName))
                {
                    searchEntity._OrderName = ReflectionTools.GetPropertyNameFromExpression<UTApplyHoliday>(x => x.InputDate);
                    searchEntity._OrderDirection = ((int)EnumOrder.DESC).ToString();
                }
                return searchEntity;
            }
            set
            {
                searchEntity = value;
            }
        }

        private IList<UTApplyHoliday> uTApplyHolidayCollection = new List<UTApplyHoliday>();
        /// <summary>
        ///  请假信息 集合
        /// </summary>
        public IList<UTApplyHoliday> UTApplyHolidayCollection
        {
            get { return uTApplyHolidayCollection; }
            set { uTApplyHolidayCollection = value; }
        }

        public void RetriveData()
        {
            ExpressionCondition<UTApplyHoliday> condition = ExpressionCondition<UTApplyHoliday>.GetInstance();
            if (string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
                condition.And(x => x.BeginTime, SearchEntity._BeginTime, ExpressionValueRelation.GreaterThanOrEqual)
                        .And(x => x.EndTime, SearchEntity._EndTime, ExpressionValueRelation.LessThanOrEqual);
            else
                condition.Or(x => x.BeginTime, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Equal)
                    .Or(x => x.EndTime, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Equal)
                    .Or(x => x.Hours, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Equal)
                    .Or(x => x.HolidayType, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
                    .Or(x => x.Remark, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like);

            //导出全部数据
            if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出全部)
            {
                UTApplyHolidayCollection = bizUTApplyHoliday.ListBy(condition.ConditionExpression, SearchEntity._OrderName, SearchEntity.EnumOrderDirection);
                ExportExcel(UTApplyHolidayCollection);
                return;
            }
            UTApplyHolidayCollection = bizUTApplyHoliday.PaginateListBy(SearchEntity._PageSize.Value, SearchEntity._PageIndex ?? 0, ref count, condition.ConditionExpression, SearchEntity._OrderName, SearchEntity.EnumOrderDirection);
            PaginateHelperObject = PaginateHelper.ConstructPaginate(SearchEntity._PageSize.Value, count, SearchEntity._PageIndex ?? 0, SearchEntity, "SearchEntity");

            //导出部分
            if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出本页)
            {
                ExportExcel(UTApplyHolidayCollection);
                return;
            }
        }

        public void ExportExcel(IList<UTApplyHoliday> datasource)
        {
            if (ExportObject.ExportType == null)
                return;
            var exportInstance = ExportHelper.GetInstance();
            exportInstance.ExportExcel<UTApplyHoliday>(datasource, ExportObject, "请假信息");
        }
    }

	/// <summary>
	/// 新增、修改、查看 请假信息
	/// </summary>
	public class ModelApplyHolidayCreate : BaseModel
	{
		private BizUTApplyHoliday bizUTApplyHoliday;

		/// <summary>
		/// 页面状态
		/// </summary>
		public EnumPageState? PageState { get; set; }

		/// <summary>
		/// 请假ID
		/// </summary>
		public Guid? UTApplyHolidayID { get; set; }

		private UTApplyHoliday uTApplyHolidayEntity = new UTApplyHoliday();
		/// <summary>
		/// 请假表 实体
		/// </summary>
		public UTApplyHoliday UTApplyHolidayEntity
		{
			get { return uTApplyHolidayEntity; }
			set { uTApplyHolidayEntity = value; }
		}

		/// <summary>
		/// 实例状态
		/// </summary>
		public WFInstanceStatus WFInstanceStatusEntity
		{
			get;
			set;
		}

		public void RetriveData()
		{
			//如果请假编号不能NULL  则查找该实体
			if (UTApplyHolidayID != null)
				UTApplyHolidayEntity = bizUTApplyHoliday.GetFirst(x => x.ID == UTApplyHolidayID);
		}

		/// <summary>
		/// 保存 新增或修改
		/// </summary>
		public void Save()
		{
			//如果是新增
			if (UTApplyHolidayEntity.ID == Guid.Empty)
			{
				UTApplyHolidayEntity.InputDate = DateTime.Now;
                UTApplyHolidayEntity.InputUser = SessionManager.CurrentSysUser.ID;
			}
			else if (UTApplyHolidayEntity.ID != Guid.Empty)
			{
                UTApplyHolidayEntity.InputDate = DateTime.Now;
                UTApplyHolidayEntity.InputUser = SessionManager.CurrentSysUser.ID;
			}
			bizUTApplyHoliday.SaveOrUpdate(UTApplyHolidayEntity);
		}
	}
}
